package com.jh.fcsm.common;

import com.jh.fcsm.beans.sys.SysRole;
import com.jh.fcsm.beans.sys.vo.LoginUser;
import com.jh.fcsm.constant.Constant;
import com.jh.fcsm.util.AppUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class BaseController {

    public static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    public static LoginUser getLoginUser() {
        Object user = AppUserUtil.getLoginAppUser();
        if (user instanceof LoginUser) {
            LoginUser appLoginUser = (LoginUser) user;
            return appLoginUser;
        }
        return null;
    }

    public static String getCurrentUserId() {
        Object user = AppUserUtil.getLoginAppUser();
        if (user instanceof LoginUser) {
            LoginUser appLoginUser = (LoginUser) user;
            return appLoginUser.getId();
        }
        return null;
    }

    public static String getCurrentUserName() {
        Object user = AppUserUtil.getLoginAppUser();
        if (user instanceof LoginUser) {
            LoginUser appLoginUser = (LoginUser) user;
            return appLoginUser.getUsername();
        }
        return null;
    }

    public static String getCurrentRealName() {
        Object user = AppUserUtil.getLoginAppUser();
        if (user instanceof LoginUser) {
            LoginUser appLoginUser = (LoginUser) user;
            return appLoginUser.getNickname();
        }
        return null;
    }

    /**
     * @return List<SysRole>
     * @Description:获取当前用户角色列表
     * @author szx
     */
    public static List<SysRole> getUserRoleList() {
        Object loginAppUser = AppUserUtil.getLoginAppUser();
        if (loginAppUser instanceof LoginUser) {
            LoginUser user = (LoginUser) loginAppUser;
            return user.getSysRoles();
        }
        return null;
    }

    /**
     * 返回当前用户的类型
     *
     * @return true 是管理员 false 非管理员
     */
    public static boolean isAdmin() {
        Object loginAppUser = AppUserUtil.getLoginAppUser();
        if (loginAppUser instanceof LoginUser) {
            LoginUser user = (LoginUser) loginAppUser;
            if (StringUtils.isEmpty(user.getType())) {
                return false;
            }
            return Constant.ADMIN.equals(user.getType());
        }
        return false;
    }

    /**
     * spring ModelAttribute 放置在方法上面：表示请求该类的每个Action前都会首先执行它，也可以将一些准备数据的操作放置在该方法里面
     */
    @ModelAttribute
    public void setBaseController(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

    public String getCurrentIp( HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

//    public static String getCurrentScienceOrgName() {
//        Object user = AppUserUtil.getLoginAppUser();
//        if (user instanceof LoginUser) {
//            LoginUser appLoginUser = (LoginUser)user;
//            return appLoginUser.getScienceOrgName();
//        }
//        return null;
//    }
//
//    public static String getCurrentManageOrgName() {
//        Object user = AppUserUtil.getLoginAppUser();
//        if (user instanceof LoginUser) {
//            LoginUser appLoginUser = (LoginUser)user;
//            return appLoginUser.getManageOrgName();
//        }
//        return null;
//    }
}
